﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Pagination;
using WMS.WarehouseManagement.PurchaseDetailss;
using WMS.WarehouseManagement.PurchaseOrders;

namespace WMS.WarehousingManagement.PurchaseDetailss
{
    /// <summary>
    /// 进货通知明细服务
    /// </summary>
    public class PurchaseDetailsAppService : ApplicationService, IPurchaseDetailsAppService
    {
        /// <summary>
        /// 进货通知明细仓储接口
        /// </summary>
        private readonly IRepository<PurchaseDetails> _purchaseDetailsrepository;

        /// <summary>
        /// 进货单号仓储接口
        /// </summary>
        private readonly IRepository<PurchaseOrder> _purchaseOrderrepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchaseDetailsrepository">进货通知明细仓储接口</param>
        /// <param name="purchaseOrderrepository">进货单号仓储接口</param>
        public PurchaseDetailsAppService(IRepository<PurchaseDetails> purchaseDetailsrepository, IRepository<PurchaseOrder> purchaseOrderrepository)
        {
            _purchaseDetailsrepository = purchaseDetailsrepository;
            _purchaseOrderrepository = purchaseOrderrepository;
        }


        /// <summary>
        /// 显示进货通知明细信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="orderName">商品名称</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> GetInquire(int pageIndex, int pageSize, string? orderName)
        {
            var purchaseDetails = await _purchaseDetailsrepository.GetListAsync(m => string.IsNullOrEmpty(orderName) || m.OrderName!.Contains(orderName));
            var purchaseOrderre = await _purchaseOrderrepository.GetListAsync();
            var list = from a in purchaseDetails
                       join b in purchaseOrderre on a.CustomerOrderNumber equals b.OrderNumber
                       select new
                       {
                           a.OrderName,
                           a.CustomerOrderNumber,
                           a.NoRegistration,
                           a.Age,
                           a.Sdate,
                           a.Unit,
                           a.DateTime,
                           a.ListingSdate,
                           b.OrderType,
                           b.Warehouse,
                           b.EstimatedTime,
                       };
            PageResult pageResult = new PageResult();
            pageResult.RowCount = list.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }





    }
}
